package com.youhome.service.service.impl;


import com.youhome.common.response.ResultPage;
import com.youhome.service.mapper.HotCityMapper;
import com.youhome.service.mapper.ZipMapper;
import com.youhome.service.model.ListHubParam;
import com.youhome.common.response.ResultCode;
import com.youhome.service.service.MSearchService;
import com.youhome.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by chen365 on 2018/2/28.
 */
@Service
public class MSearchServiceImpl implements MSearchService {

    @Autowired
    private ZipMapper zipMapper;

    @Autowired
    private HotCityMapper hotCityMapper;

    @Override
    public ResultPage checkoutAction(String action, String keyword) {
        if (action.equalsIgnoreCase("zipcode")) {
            if (keyword.length() != 5) {
                //"非法的郵編地址"
                return new ResultPage(ResultCode.HOUSE_ILLEGALITY_ZIPCODE);
            }
        } else if (action.equalsIgnoreCase("keyword")) {
            if (keyword.length() < 3) {
                //return new ErrorResult("地址搜索必须要三个字母以上");
                return new ResultPage(ResultCode.HOUSE_ADDRESS_LENGTH_SHORTAGE);
            }
        } else if (action.equalsIgnoreCase("city")) {
            if (keyword.length() < 3) {
                //return new ErrorResult("城市搜索必须要三个字母以上");
                return new ResultPage(ResultCode.HOUSE_CITY_LENGTH_SHORTAGE);
            }
        } else {
            //return new ErrorResult("不支持的搜索类型");
            return new ResultPage(ResultCode.UNSUPPORTED_SEARCH_TYPE);
        }
        return null;
    }

    @Override
    public ListHubParam initListHubParam(
            String action, String keyword, String type,
            Integer beds, Integer baths, String year, String minSize, String maxSize,
            String minPrice, String maxPrice,
            String filed, String direction, String status, Boolean isStar, Boolean isCheck) {
        ListHubParam param = new ListHubParam();
        if (action.equalsIgnoreCase("zip")) {
            param.setZip(keyword);
        } else if (action.equalsIgnoreCase("address")) {
            param.setAddress(keyword);
        } else if (action.equalsIgnoreCase("city")) {
            List<String> zips = new ArrayList<>();
            if (keyword.equals("San Francisco") ||
                    keyword.equals("Los Angeles") ||
                    keyword.equals("New York") ||
                    keyword.equals("Seattle") ||
                    keyword.equals("Atlanta") ||
                    keyword.equals("Boston") ||
                    keyword.equals("New Jersey") ||
                    keyword.equals("Houston") ||
                    keyword.equals("San Diago") ||
                    keyword.equals("Orlando")
                    ) {
                String zipcodes = hotCityMapper.selectZipsByEnName(keyword);
                zips = Arrays.asList(zipcodes.split(","));
            } else {
                zips = zipMapper.selectZipsByCity(keyword);
            }


            if (zips.size() > 0) {
                param.setZips(zips);
            } else {
                param.setCity(keyword);
            }
        }

        if (!StringUtils.isEmpty(type)) {
            List<String> propertySubtypes = new ArrayList<>();
            String[] arrStr = type.split(",");
            if (arrStr.length > 1) {
                for (String item : arrStr) {
                    propertySubtypes.add(item);
                }
                param.setPropertySubtypes(propertySubtypes);
            } else {
                param.setPropertySubtype(type);
            }
        }

        if (!StringUtils.isEmpty(beds)) {
            param.setBedroomCount(beds);
        }
        if (!StringUtils.isEmpty(baths)) {
            param.setBathroomCount(baths);
        }
        if (!StringUtils.isEmpty(year)) {
            param.setYearBuilt(Integer.valueOf(year));
        }
        if (!StringUtils.isEmpty(minSize)) {
            param.setMinLivingArea(Double.valueOf(minSize));
        }
        if (!StringUtils.isEmpty(maxSize)) {
            param.setMaxLivingArea(Double.valueOf(maxSize));
        }
        if (!StringUtils.isEmpty(minPrice)) {
            param.setMinListPrice(Double.valueOf(minPrice));
        }
        if (!StringUtils.isEmpty(maxPrice)) {
            param.setMaxListPrice(Double.valueOf(maxPrice));
        }
        if (!StringUtils.isEmpty(status)) {
            param.setListingStatus(status);
        }
        if (!StringUtils.isEmpty(isStar)) {
            param.setIsStar(isStar);
        }
        if (!StringUtils.isEmpty(isCheck)) {
            param.setIsCheck(isCheck);
        }

        if (!StringUtils.isEmpty(filed)) {
            param.setOrderBy(StringUtils.transitionOrderBy(filed) + " " + direction);
        }
        return param;
    }

    @Override
    public ListHubParam initListHubParam(String action, String keyword, String type, Integer beds, Integer baths, String year, String minSize, String maxSize, String minPrice, String maxPrice, String filed, String direction, String status, Boolean isStar, Boolean isCheck, Double minX, Double maxX, Double minY, Double maxY) {

        ListHubParam params = initListHubParam(action, keyword, type, beds, baths, year, minSize, maxSize, minPrice, maxPrice, filed, direction, status, isStar, isCheck);
        if (!StringUtils.isEmpty(minX)) {
            params.setMinX(minX);
        }
        if (!StringUtils.isEmpty(maxX)) {
            params.setMaxX(maxX);
        }
        if (!StringUtils.isEmpty(minY)) {
            params.setMinY(minY);
        }
        if (!StringUtils.isEmpty(maxY)) {
            params.setMaxY(maxY);
        }
        params.setEnabled(true);
        return params;
    }
}
